home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / guest-0.000 / guest-0 / guest-0.2 / main.c < prev    next >
C/C++ Source or Header  |  1995-05-08  |  3KB  |  111 lines

  1. /*
  2. This is a program to display menus on a text tty, good for limited access
  3. guest accounts, or just for user menus.
  4. Copyright (C) 1995  Brian Cully
  5.  
  6. This program is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free Software
  8. Foundation; either version 2 of the License, or (at your option) any later
  9. version.
  10.  
  11. This program is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  14. details.
  15.  
  16. You should have received a copy of the GNU General Public License along with
  17. this program; see the file COPYING. If not write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. please send patches or advice to: `shmit@meathook.intac.com'
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <fcntl.h>
  26. #include <unistd.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29.  
  30. /* Include configuration data */
  31. #include "config.h"
  32.  
  33. #include "parse.h"
  34. #include "screen.h"
  35.  
  36. char progname[255];
  37. char *menufile;
  38.  
  39. void setupfile(const char *);
  40.  
  41. int main(int argc, char *argv[]) {
  42.    struct menu *guest_menu;
  43.    
  44.    strcpy(progname, argv[0]);
  45.  
  46.    if (argc > 2) {
  47.       fprintf(stderr, "usage: %s [menu-file]\n", progname);
  48.       exit(1);
  49.    }
  50.  
  51.    if (argc < 2) {
  52. #if defined (DEBUG)
  53.       printf("%s\n", DEFAULT_MENU);
  54. #endif
  55.       setupfile(DEFAULT_MENU);
  56.    } else
  57.       /* Load file into buffer */
  58.       setupfile(argv[1]);
  59.  
  60.    /* Parse File */
  61.    if (parsefile(&guest_menu, menufile) == -1) {
  62.       fprintf(stderr,"%s: error while parsing\n",progname);
  63.       freemem(guest_menu, menufile);
  64.       exit(1);
  65.    }
  66.  
  67.    init_scr();
  68.    display_list(guest_menu);
  69.    close_scr();
  70.    
  71.    /* Free all allocated memory */
  72.    freemem(guest_menu, menufile);
  73.  
  74.    /* we outtie */
  75.    exit(0);
  76. }
  77.  
  78. void setupfile(const char *filen) {
  79.    int fd;
  80.    struct stat statbuff;
  81.  
  82.   /* Load Files, get stats, and malloc() buffer, read file into buffer */
  83.  
  84.    if ((fd = open(filen, O_RDONLY)) == -1) {
  85.       fprintf(stderr, "%s: Couldn't open file %s for reading\n", progname, filen);
  86.       exit(1);
  87.    }
  88.  
  89.    if (fstat(fd, &statbuff) == -1) {
  90.       fprintf(stderr, "%s: Couldn't get statistics on file %s\n", progname, filen);
  91.       exit(1);
  92.    }
  93.  
  94.    if (statbuff.st_size == 0) {
  95.       fprintf(stderr, "%s: %s is an empty file\n", progname, filen);
  96.       exit(1);
  97.    }
  98.  
  99.    if ((menufile = (char *)malloc(statbuff.st_size)) == NULL) {
  100.       fprintf(stderr, "%s: Couldn't allocate %i bytes from heap\n", progname, (int)statbuff.st_size);
  101.       exit(1);
  102.    }
  103.  
  104.    if (read(fd, menufile, statbuff.st_size) == -1) {
  105.       fprintf(stderr, "%s: Couldn't read file %s\n", progname, filen);
  106.       exit(1);
  107.    }
  108.  
  109.    close(fd);
  110. }
  111.